<?php

namespace App\Admin\Controllers;

use App\Models\Administrator;
use App\Models\Proxy;
use App\Models\Team;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;

class TeamsController extends Controller
{
    use ModelForm;

    private $header = '团体管理';
    private $description = '管理所有地区的团体';

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->header);
            $content->description($this->description);

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {
            $content->header($this->header);
            $content->description($this->description);

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {
            $content->header($this->header);
            $content->description($this->description);

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Team::class, function (Grid $grid) {
            $grid->name('名称');
            $grid->description('描述');
            $grid->uuid('UUID');
            $grid->discount('折扣')->display(function ($discount) {
                if ($discount == 0) {
                    return '无折扣';
                } else {
                    return ($discount * 10) . '折';
                }
            });
            $grid->free_times('咖啡券')->display(function ($free_times) {
                if ($free_times == 0) {
                    return '无';
                } else {
                    return $free_times . '张';
                }
            });

            $grid->filter(function ($filter) {
                // 去掉默认的id过滤器
                $filter->disableIdFilter();
            });
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(Team::class, function (Form $form) {

            $form->text('name', '名称')->rules('required');
            $form->text('description', '描述')->rules('required');
            $form->image('avatar', 'logo')->rules('required');
            $form->number('discount', '折扣')->default(0)->rules('required');
            $form->number('free_times', '免费咖啡')->default(0)->rules('required');
        });
    }
}
